#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#define FILE_NAME "log.txt"
//#define FILE_NAME(number) "log.txt"#number
//标志位传参 让每个比特位上的1不重叠
//#define ONE (1<<1)
//#define TWO (1<<2)
//#define THREE (1<<3)
//#define FOUR (1<<4)
 //标志位传参
//void show(int flags)
//{
//    if(flags & ONE) printf("ONE\n");
//    if(flags & TWO) printf("TWO\n");
//    if(flags & THREE) printf("THREE\n");
//    if(flags & FOUR) printf("FOUR\n");
//}
int main()
{
   // "a" 
   int fd = open(FILE_NAME, O_WRONLY | O_CREAT | O_APPEND, 0666);

    int cnt = 5;
    char Buffer[64];
    while(cnt)
    {
        //格式化字符数组
        sprintf(Buffer,"%s:%d\n","hello world",cnt--);
        write(fd,Buffer,strlen(Buffer));//strlen不带+1

    }
    close(fd);





    //O_WRONLY -- 只读， O_CREAT -- 没有该文件时自动创建 O_TRUNC -- 文件内容清空
   //int fd = open(FILE_NAME,O_WRONLY | O_CREAT | O_TRUNC,0666);
    //int fd = open(FILE_NAME,O_RDONLY);
    //if(fd < 0)
    //{
    //    perror("open");
    //    return 1;
    //}

    //char Buffer[10];
    //ssize_t num = read(fd,Buffer,sizeof(Buffer)-1);
    //if(num >0) Buffer[num] = 0;
    //printf("%s\n",Buffer);
   // int cnt = 5;
   // char Buffer[64];
   // while(cnt)
   // {
   //     //格式化字符数组
   //     sprintf(Buffer,"%s:%d\n","hello world",cnt--);
   //     write(fd,Buffer,strlen(Buffer));//strlen不带+1
   // }

    
    


//    //标志位传参
//    show(ONE);
//    printf("-----------\n");
//    show(TWO);
//    printf("-----------\n");
//    show(THREE | FOUR);
//    printf("-----------\n");
//    show(ONE | TWO | THREE | FOUR);
//    printf("-----------\n");

    //不传入路径默认在当前路径
    //FILE* fp = fopen(FILE_NAME,"w");
    //FILE* fp = fopen(FILE_NAME,"w");

    //if(fp == NULL)
    //{
    //    perror("fopen");
    //    return 1;
    //}
    //char buffer[64];
    //while(fgets(buffer,sizeof(buffer)-1,fp))
    //{
    //    buffer[strlen(buffer)-1] = 0;
    //    puts(buffer);
    //}
    //int cnt = 5;
    //while(cnt--)
    //{
    //    fprintf(fp,"%s:%d\n","hello world",cnt);
    //}
    //fclose(fp);
    return 0;
}
